package com.jiugang.cardump.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jiugang.cardump.bean.InformationBean;
import com.jiugang.cardump.constant.StatusCodeConstant;
import com.jiugang.cardump.entity.Equipment;
import com.jiugang.cardump.entity.Pro;
import com.jiugang.cardump.entity.ProEquRef;
import com.jiugang.cardump.exception.SystemExceptionV110;
import com.jiugang.cardump.mapper.EquipmentMapper;
import com.jiugang.cardump.mapper.ProEquRefMapper;
import com.jiugang.cardump.mapper.ProMapper;
import com.jiugang.cardump.pojo.ProEquPOJO;
import com.jiugang.cardump.result.ResultInfoV110;
import com.jiugang.cardump.service.ProService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2021-08-19
 */
@Service
public class ProServiceImpl extends ServiceImpl<ProMapper, Pro> implements ProService {

    @Autowired
    ProMapper proMapper;
    @Autowired
    ProEquRefMapper proEquRefMapper;
    @Autowired
    EquipmentMapper equipmentMapper;


    /**
     * @param proEquPOJO
     * @return
     * @Author luoxin
     * @Description 添加项目接口
     * @Date 3:31 下午 2021/8/20
     */
    @Override
    public ResultInfoV110 insert(ProEquPOJO proEquPOJO) {
        log.debug("ProServiceImpl的insert方法进入，参数为" + proEquPOJO);

        //解析传入的数据
        Pro pro = proEquPOJO.getPro();
        Integer[] equIds = proEquPOJO.getEquIds();

        //调用mapper根据传入的项目信息，添加项目信息
        int proInsert = proMapper.insert(pro);
        //验证是否成功插入数据
        if (proInsert == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "添加项目数据"));
        }

        //取得添加的项目信息id
        Integer id = pro.getId();
        //根据传入的项目设备Id数组，建立项目与设备关联信息
        for (int i = 0; i < equIds.length; i++) {
            ProEquRef proEquRef = new ProEquRef();
            proEquRef.setProId(id);
            //根据索引得出数组内对应信息
            Integer s = equIds[i];
            proEquRef.setEquTableId(s);
            //调用mapper，插入项目设备信息
            int proEquRefInsert = proEquRefMapper.insert(proEquRef);
            //验证是否成功插入数据
            if (proEquRefInsert == 0) {
                throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "添加项目设备数据"));
            }
        }
        log.debug("ProServiceImpl的insert方法结束");
        return ResultInfoV110.success();
    }

    /**
     * @param proEquPOJO
     * @return
     * @Author luoxin
     * @Description 修改项目接口
     * @Date 3:32 下午 2021/8/20
     */
    @Override
    public ResultInfoV110 update(ProEquPOJO proEquPOJO) {
        log.debug("ProServiceImpl的update方法进入，参数为" + proEquPOJO);

        //解析传入的数据
        Pro pro = proEquPOJO.getPro();
        Integer[] equIds = proEquPOJO.getEquIds();

        //调用mapper修改项目信息
        int updateProById = proMapper.updateById(pro);
        //验证是否成功修改数据
        if (updateProById == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "修改项目数据"));
        }

        //取得传入项目的Id，根据ID删除有关设备信息
        Integer proId = pro.getId();
        QueryWrapper<ProEquRef> proEquRefQueryWrapper = new QueryWrapper<>();
        proEquRefQueryWrapper.eq("pro_id", proId);
        int deleteProEquRef = proEquRefMapper.delete(proEquRefQueryWrapper);
        //验证是否成功删除数据
        if (deleteProEquRef == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "删除项目设备数据"));
        }

        //根据传入的项目设备ID数组，重新添加项目设备数据
        for (int i = 0; i < equIds.length; i++) {
            ProEquRef proEquRef = new ProEquRef();
            proEquRef.setProId(proId);
            //根据索引得出数组内对应信息
            Integer s = equIds[i];
            proEquRef.setEquTableId(s);
            //调用mapper添加项目设备数据信息
            int insertProEquRef = proEquRefMapper.insert(proEquRef);
            //验证是否成功插入数据
            if (insertProEquRef == 0) {
                throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "修改项目设备数据"));
            }
        }

        log.debug("ProServiceImpl的update方法结束");
        return ResultInfoV110.success();
    }


    /**
     * @param id，statu
     * @return
     * @Author luoxin
     * @Description 项目启动，关闭接口
     * @Date 3:32 下午 2021/8/20
     */
    @Override
    public ResultInfoV110 updateStatus(Integer id, Integer status) {
        log.debug("ProServiceImpl的updateStatus方法进入，参数为" + id);

        //根据传入信息，调用mapper修改项目启动状态
        Pro pro = new Pro();
        pro.setId(id);
        pro.setStatu(status);
        proMapper.updateById(pro);

        log.debug("ProServiceImpl的updateStatus方法结束");
        return ResultInfoV110.success();
    }


    /**
     * @param
     * @return
     * @Author luoxin
     * @Description 查询项目列表接口
     * @Date 3:33 下午 2021/8/20
     */
    @Override
    public ResultInfoV110 select(Pro pro) {
        log.debug("ProServiceImpl的select方法进入");
        QueryWrapper<Pro> proQueryWrapper = new QueryWrapper<>();
        proQueryWrapper.like((pro.getProName() != null), "pro_name", pro.getProName())
                .like((pro.getProAddr() != null), "pro_addr", pro.getProAddr())
                .like((pro.getProLead() != null), "pro_lead", pro.getProLead())
                .orderByDesc("datetime");
        List<Pro> pros = proMapper.selectList(proQueryWrapper);
        log.debug("ProServiceImpl的select方法结束");
        return ResultInfoV110.success(pros);

    }

    @Override
    public ResultInfoV110 selectById(Integer id) {
        log.debug("ProServiceImpl的selectById方法进入，参数为" + id);
        Pro pro = proMapper.selectById(id);
        log.debug("ProServiceImpl的selectById方法结束");
        return ResultInfoV110.success(pro);
    }

    @Override
    public ResultInfoV110 delete(Integer id) {
        int deleteById = proMapper.deleteById(id);
        //验证是否成功插入数据
        if (deleteById == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "删除项目"));
        }

        QueryWrapper<ProEquRef> proEquRefQueryWrapper = new QueryWrapper<>();
        proEquRefQueryWrapper.eq("pro_id", id);
        proEquRefMapper.delete(proEquRefQueryWrapper);
        return ResultInfoV110.success();
    }


}
